<template>
  <div class="service-type-management">
    <el-card>
      <div class="card-header">
        <span>服务类型管理</span>
        <div class="header-actions">
          <el-button type="primary" @click="handleAdd">
            <i class="el-icon-plus"></i> 添加服务类型
          </el-button>
          <el-button @click="handleRefresh">
            <i class="el-icon-refresh"></i> 刷新
          </el-button>
        </div>
      </div>
      
      <el-form :inline="true" class="search-form" :model="searchForm" @submit.native.prevent>
        <el-form-item label="类型名称">
          <el-input v-model="searchForm.typeName" placeholder="请输入服务类型名称" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
      
      <el-table :data="serviceTypeList" style="width: 100%" v-loading="loading">
        <el-table-column prop="typeName" label="服务类型名称" min-width="200"></el-table-column>
        <el-table-column prop="description" label="描述" min-width="300">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <p>{{ scope.row.description || '无描述' }}</p>
              <div slot="reference" class="description-tooltip">
                {{ scope.row.description || '无描述' }}
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" min-width="160"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" min-width="160"></el-table-column>
        <el-table-column label="操作" width="80" fixed="right">
          <template slot-scope="scope">
            <el-dropdown trigger="click" @command="handleCommand">
              <span class="el-dropdown-link">
                <i class="el-icon-setting"></i>
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item :command="{ type: 'edit', data: scope.row }">编辑</el-dropdown-item>
                <el-dropdown-item :command="{ type: 'delete', data: scope.row }" type="danger">删除</el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      
      <div class="pagination-container">
        <el-pagination
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="currentPage"
          :page-size="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        ></el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'ServiceTypeManagementList',
  data() {
    return {
      serviceTypeList: [],
      total: 0,
      currentPage: 1,
      pageSize: 10,
      loading: false,
      searchForm: {
        typeName: ''
      }
    };
  },
  mounted() {
    this.loadServiceTypes();
  },
  methods: {
    loadServiceTypes() {
      this.loading = true;
      const params = {
        page: this.currentPage,
        pageSize: this.pageSize,
        typeName: this.searchForm.typeName
      };
      
      this.$api.serviceTypeApi.getAll(params)
        .then(response => {
          // http.js已经处理了响应格式，确保返回数组
          this.serviceTypeList = response && Array.isArray(response.data) ? response.data : [];
          this.total = response && response.total || 0;
          this.loading = false;
        })
        .catch(error => {
          console.error('获取服务类型列表失败:', error);
          this.$message.error('获取服务类型列表失败');
          this.loading = false;
        });
    },
    
    handleSearch() {
      this.currentPage = 1;
      this.loadServiceTypes();
    },
    
    handleReset() {
      this.searchForm = {
        typeName: ''
      };
      this.currentPage = 1;
      this.loadServiceTypes();
    },
    
    handleAdd() {
      this.$router.push('/service-type-management/add');
    },
    
    handleEdit(row) {
      this.$router.push({
        path: '/service-type-management/edit',
        query: { id: row.id }
      });
    },
    
    handleDelete(row) {
      this.$confirm('确定要删除该服务类型吗？删除后将影响使用该类型的服务。', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$api.serviceTypeApi.delete(row.id)
            .then(data => {
              // http.js已经处理了响应格式，不需要额外判断
              this.$message.success('删除成功');
              this.loadServiceTypes();
            })
          .catch(error => {
            this.$message.error('删除失败: ' + error.message);
          });
      }).catch(() => {
        this.$message.info('已取消删除');
      });
    },
    
    handleCommand(command) {
      if (command.type === 'edit') {
        this.handleEdit(command.data);
      } else if (command.type === 'delete') {
        this.handleDelete(command.data);
      }
    },
    
    handleSizeChange(size) {
      this.pageSize = size;
      this.loadServiceTypes();
    },
    
    handleCurrentChange(current) {
      this.currentPage = current;
      this.loadServiceTypes();
    },
    
    handleRefresh() {
      this.loadServiceTypes();
    }
  }
};
</script>

<style scoped>
.service-type-management {
  padding: 20px;
}

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 18px;
  font-weight: bold;
  margin-bottom: 20px;
}

.header-actions {
  display: flex;
}

.search-form {
  margin-bottom: 20px;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.description-tooltip,
.ip-ranges-tooltip {
  cursor: pointer;
  color: #409EFF;
}
</style>